using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SystemFunctionLib.Personalization;

namespace SystemFunctionDemo
{
    public partial class DisplayForm : Form
    {
        public DisplayForm()
        {
            InitializeComponent();

            this.Font = SystemFonts.DialogFont;

            LoadSettings();
        }

        private void LoadSettings()
        {
            LoadFontSettings();
            LoadDesktopSettings();
            LoadMenuSettings();
            LoadScreenSaverSettings();
        }

        private void LoadFontSettings()
        {
            switch (Display.FontSmoothingType)
            {
                case FontSmoothingType.None:
                    cmbFontSmoothing.SelectedIndex = 0;
                    break;
                case FontSmoothingType.Standard:
                    cmbFontSmoothing.SelectedIndex = 1;
                    break;
                case FontSmoothingType.ClearType:
                    cmbFontSmoothing.SelectedIndex = 2;
                    break;
            }

            sliderClearTypeContrast.Maximum = Display.FontSmoothingContrastMaxValue;
            sliderClearTypeContrast.Minimum = Display.FontSmoothingContrastMinValue;
            sliderClearTypeContrast.Value = Display.FontSmoothingContrast;

            switch (Display.FontSmoothingOrientation)
            {
                case FontSmoothingOrientation.Rgb:
                    cmbClearTypeOrientation.SelectedIndex = 0;
                    break;
                case FontSmoothingOrientation.Bgr:
                    cmbClearTypeOrientation.SelectedIndex = 1;
                    break;
            }
        }

        private void LoadDesktopSettings()
        {
            string wallpaper = Display.DesktopWallpaper;
            if (wallpaper == string.Empty)
            {
                chkUseWallpaper.Checked = false;
            }
            else
            {
                chkUseWallpaper.Checked = true;
            }
            txtWallpaper.Text = Display.DesktopWallpaper;

            Rectangle screenRect = Screen.PrimaryScreen.Bounds;
            numWorkAreaLeft.Minimum = numWorkAreaRight.Minimum = screenRect.Left;
            numWorkAreaLeft.Maximum = numWorkAreaRight.Maximum = screenRect.Right;
            numWorkAreaTop.Minimum = numWorkAreaBottom.Minimum = screenRect.Top;
            numWorkAreaTop.Maximum = numWorkAreaBottom.Maximum = screenRect.Bottom;

            Rectangle workAreaRect = Display.GetWorkArea();
            numWorkAreaLeft.Value = workAreaRect.Left;
            numWorkAreaTop.Value = workAreaRect.Top;
            numWorkAreaRight.Value = workAreaRect.Right;
            numWorkAreaBottom.Value = workAreaRect.Bottom;
        }

        private void LoadMenuSettings()
        {
            chkDropShadow.Checked = Display.ShowMenuDropShadow;
            chkFlatMenu.Checked = Display.ShowFlatMenu;

            switch (Display.MenuDropAlignment)
            {
                case MenuDropAlignment.Left:
                    radioDropAlignmentLeft.Checked = true;
                    break;
                case MenuDropAlignment.Right:
                    radioDropAlignmentRight.Checked = true;
                    break;
            }

            switch (Display.MenuAnimation)
            {
                case MenuAnimation.None:
                    cmbMenuAnimation.SelectedIndex = 0;
                    break;
                case MenuAnimation.Slide:
                    cmbMenuAnimation.SelectedIndex = 1;
                    break;
                case MenuAnimation.Fade:
                    cmbMenuAnimation.SelectedIndex = 2;
                    break;
            }

            numMenuShowDelay.Value = Display.MenuShowDelay;
        }

        private void LoadScreenSaverSettings()
        {
            chkIsScreenSaverEnabled.Checked = Display.IsScreenSaverEnabled;
            chkSecureScreenSaver.Checked = Display.SecureScreenSaver;
            int timeout = Display.ScreenSaverTimeOut;
            numScreenSaverTimeOutMinutes.Value = timeout / 60;
            numScreenSaverTimeOutSeconds.Value = timeout % 60;
        }

        private void SaveSettings()
        {
            SaveFontSettings();
            SaveDesktopSettings();
            SaveMenuSettings();
            SaveScreenSaverSettings();
        }

        private void SaveFontSettings()
        {
            switch (cmbFontSmoothing.SelectedIndex)
            {
                case 0:
                    Display.FontSmoothingType = FontSmoothingType.None;
                    break;
                case 1:
                    Display.FontSmoothingType = FontSmoothingType.Standard;
                    break;
                case 2:
                    Display.FontSmoothingType = FontSmoothingType.ClearType;
                    break;
            }

            Display.FontSmoothingContrast = sliderClearTypeContrast.Value;

            switch (cmbClearTypeOrientation.SelectedIndex)
            {
                case 0:
                    Display.FontSmoothingOrientation = FontSmoothingOrientation.Rgb;
                    break;
                case 1:
                    Display.FontSmoothingOrientation = FontSmoothingOrientation.Bgr;
                    break;
            }
        }

        private void SaveDesktopSettings()
        {
            if (chkUseWallpaper.Checked)
            {
                Display.DesktopWallpaper = txtWallpaper.Text;
            }
            else
            {
                Display.DesktopWallpaper = string.Empty;
            }

            Rectangle workAreaRect = Rectangle.FromLTRB(
                (int)numWorkAreaLeft.Value,
                (int)numWorkAreaTop.Value,
                (int)numWorkAreaRight.Value,
                (int)numWorkAreaBottom.Value);
            Display.SetWorkArea(workAreaRect);
        }

        private void SaveMenuSettings()
        {
            Display.ShowMenuDropShadow = chkDropShadow.Checked;
            Display.ShowFlatMenu = chkFlatMenu.Checked;

            if (radioDropAlignmentLeft.Checked)
            {
                Display.MenuDropAlignment = MenuDropAlignment.Left;
            }
            else if (radioDropAlignmentRight.Checked)
            {
                Display.MenuDropAlignment = MenuDropAlignment.Right;
            }

            switch (cmbMenuAnimation.SelectedIndex)
            {
                case 0:
                    Display.MenuAnimation = MenuAnimation.None;
                    break;
                case 1:
                    Display.MenuAnimation = MenuAnimation.Slide;
                    break;
                case 2:
                    Display.MenuAnimation = MenuAnimation.Fade;
                    break;
            }

            Display.MenuShowDelay = (int)numMenuShowDelay.Value;
        }

        private void SaveScreenSaverSettings()
        {
            Display.IsScreenSaverEnabled = chkIsScreenSaverEnabled.Checked;
            Display.SecureScreenSaver = chkSecureScreenSaver.Checked;
            Display.ScreenSaverTimeOut = 
                (int)numScreenSaverTimeOutMinutes.Value * 60 +
                (int)numScreenSaverTimeOutSeconds.Value;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            SaveSettings();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void btnBrowseWallpaper_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.FileName = txtWallpaper.Text;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtWallpaper.Text = dlg.FileName;
            }
        }

        private void txtWallpaper_TextChanged(object sender, EventArgs e)
        {
            if (txtWallpaper.Text != string.Empty)
            {
                chkUseWallpaper.Checked = true;
            }
        }

        private void btnUseDefaultClearTypeContrast_Click(object sender, EventArgs e)
        {
            sliderClearTypeContrast.Value = Display.FontSmoothingContrastDefault;
        }

        private void sliderClearTypeContrast_ValueChanged(object sender, EventArgs e)
        {
            lblClearTypeContrast.Text = sliderClearTypeContrast.Value.ToString();
        }
    }
}